package net.sk.china.front.service.impl.product;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sk.china.common.exception.TalkException;
import net.sk.china.common.model.product.StoreProductAttrValue;
import net.sk.china.front.dao.product.StoreProductAttrValueDao;
import net.sk.china.front.service.product.StoreProductAttrValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/12 16:44
 * +----------------------------------------------------------------------
 */

@Service
public class StoreProductAttrValueServiceImpl extends ServiceImpl<StoreProductAttrValueDao, StoreProductAttrValue> implements StoreProductAttrValueService {

    @Resource
    private StoreProductAttrValueDao dao;

    /**
     * 获取商品规格列表
     * @param productId 商品id
     * @param type 商品类型
     * @return {@code List<StoreProductAttrValue>}
     */
    @Override
    public List<StoreProductAttrValue> getListByProductIdAndType(Integer productId, Integer type) {
        LambdaQueryWrapper<StoreProductAttrValue> query = new LambdaQueryWrapper<>();
        query.eq(StoreProductAttrValue::getProductId, productId);
        query.eq(StoreProductAttrValue::getType, type);
        query.eq(StoreProductAttrValue::getIsDel, false);

        return dao.selectList(query);
    }

    /**
     * 根据商品id和attrId获取列表集合
     * @param productId 商品id
     * @param attrId 属性id
     * @param type 商品类型
     * @return {@code List<StoreProductAttrValue>}
     */
    @Override
    public List<StoreProductAttrValue> getListByProductIdAndAttrId(Integer productId, String attrId, Integer type) {
        LambdaQueryWrapper<StoreProductAttrValue> query = new LambdaQueryWrapper<>();
        query.eq(StoreProductAttrValue::getProductId, productId);
        query.eq(StoreProductAttrValue::getType, type);
        if(null != attrId){
            query.eq(StoreProductAttrValue::getId, attrId);
        }
        query.eq(StoreProductAttrValue::getIsDel, false);
        return dao.selectList(query);
    }

    /**
     * 根据id、类型查询
     * @param id ID
     * @param type 类型
     * @return {@code StoreProductAttrValue}
     */
    @Override
    public StoreProductAttrValue getByIdAndProductIdAndType(Integer id, Integer productId, Integer type) {
        LambdaQueryWrapper<StoreProductAttrValue> query = new LambdaQueryWrapper<>();
        query.eq(StoreProductAttrValue::getId, id).eq(StoreProductAttrValue::getProductId, productId).eq(StoreProductAttrValue::getType, type)
                .eq(StoreProductAttrValue::getIsDel, false);

        return dao.selectOne(query);
    }

    /**
     * 添加/扣减库存
     * @param id 秒杀商品id
     * @param num 数量
     * @param operationType 类型：add—添加，sub—扣减
     * @param type 活动类型 0=商品，1=秒杀，2=砍价，3=拼团
     * @return {@code boolean}
     */
    @Override
    public boolean operationStock(Integer id, Integer num, String operationType, Integer type) {
        UpdateWrapper<StoreProductAttrValue> updateWrapper = new UpdateWrapper<>();
        if (operationType.equals("add")) {
            updateWrapper.setSql(StrUtil.format("stock = stock + {}", num));
            updateWrapper.setSql(StrUtil.format("sales = sales - {}", num));
            if (type > 0) {
                updateWrapper.setSql(StrUtil.format("quota = quota + {}", num));
            }
        }
        if (operationType.equals("sub")) {
            updateWrapper.setSql(StrUtil.format("stock = stock - {}", num));
            updateWrapper.setSql(StrUtil.format("sales = sales + {}", num));
            if (type > 0) {
                updateWrapper.setSql(StrUtil.format("quota = quota - {}", num));
                // 扣减时加乐观锁保证库存不为负
                updateWrapper.last(StrUtil.format("and (quota - {} >= 0)", num));
            } else {
                // 扣减时加乐观锁保证库存不为负
                updateWrapper.last(StrUtil.format("and (stock - {} >= 0)", num));
            }
        }
        updateWrapper.eq("id", id);
        updateWrapper.eq("type", type);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new TalkException("更新商品attrValue失败，attrValueId = " + id);
        }
        return true;
    }
}
